package com.i2bt.util.tree;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Title : 
 *
 * Description : 
 *
 * CreatedTime : 2014-10-12
 *
 * @author wuliao & kakaxi & yi & flame
 *
 * @version 1.0
 *
 */
public class Tree<T> implements ITree<T>{
	
	/**
	 * 根节点
	 */
	private Node<T> root;
	
	/**
	 * 结点指针
	 */
	private Node<T> cursor;
	
    public Tree() {
    	this(new Node<T>());
    }
    
    public Tree(Node<T> root) {
    	this.root = root;
    	cursor = root;
    }
    
    public void setRoot(Node<T> root) {
    	this.root = root;
    }
    
    public Node<T> getRoot() {
    	return root;
    }
    
    /**
     * 获取最后一个结点
     * @return
     */
    private Node<T> getLast() {
    	Node<T> lastNode = cursor.getLeftNode();
    	
    	while (lastNode.getRightNode() != null) {
    		lastNode = lastNode.getRightNode();
    	}
    	return lastNode;
    }
    
    /**
     * 获取指定结点
     * @param index 索引
     * @return
     */
    private Node<T> getLocalNode(int index) {
    	Node<T> tempNode = cursor.getLeftNode();
    	
    	if (index == 0) {
        	return tempNode;
    	}
    	
    	for (int i = 0; i < index; i++) {
    		tempNode = tempNode.getRightNode();
    	}
    	
    	return tempNode;
    }

	@Override
	public void add(T t) {
		if (t == null) {
			try {
				throw new Throwable("目录信息为空");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return;
		}
		
		Node<T> node = new Node<T>(t);
		node.setParentNode(cursor);
		if (cursor.getLeftNode() == null) {
			cursor.setLeftNode(node);
		}
		else {
			Node<T> lastNode = getLast();
			lastNode.setRightNode(node);
		}
		
		cursor.addLength();
		
	}

	@Override
	public void add(ArrayList<T> list) {
		if (list == null) {
			try {
				throw new Throwable("目录链表为空");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return;
		}
		
		for (int i = 0; i < list.size(); i++) {
			T data = list.get(i);
			add(data);
		}
		
	}

	@Override
	public void delete(String s) {
		
	}

	@Override
	public void delete(int index) {
		if (index < 0 || index > cursor.getLength()) {
			try {
				throw new Throwable("超出索引");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return;
		}
		
		Node<T> preNode = null;
		if (index == 0) {
			preNode = cursor;
			preNode.setLeftNode(preNode.getLeftNode().getRightNode());
		}
		else {
			preNode = getLocalNode(index - 1);
			preNode.setRightNode(preNode.getRightNode().getRightNode());
		}
		
	}

	@Override
	public void delete(ArrayList<Integer> list) {
		if (list == null) {
			try {
				throw new Throwable("链表为空!");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return;
		}
		
		for (int i = 0; i < list.size(); i++) {
			int index = list.get(i);
			delete(index);
		}
	}

	@Override
	public T get() {
		T data = cursor.getData();
		return data;
	}

	@Override
	public List<T> getAll() {
		if (cursor.getLength() == 0) {
			return null;
		}
		
		ArrayList<T> nodeList = new ArrayList<T>();
		Node<T> tempNode = cursor.getLeftNode();
		nodeList.add(tempNode.getData());
		
		for (int i = 1; i < cursor.getLength(); i++) {
			tempNode = tempNode.getRightNode();
			T data = tempNode.getData();
			nodeList.add(data);
		}
		
		return nodeList;
	}

	@Override
	public T getChild(String s) {
		return null;
	}

	@Override
	public T getChild(int index) {
		if (index < 0 || index > cursor.getLength()) {
			try {
				throw new Throwable("超出索引范围!");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return null;
		}
		
		Node<T> tempNode = getLocalNode(index);
		T data = tempNode.getData();
		
		return data;
	}

	@Override
	public void replace(String s, T t) {

	}

	@Override
	public void replace(int index, T t) {
		if (index < 0 || index > cursor.getLength()) {
			try {
				throw new Throwable("超出索引范围!");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return;
		}
		
		Node<T> tempNode = getLocalNode(index);
		tempNode.setData(t);
	}

	@Override
	public void enter(String s) {
		
	}

	@Override
	public void enter(int index) {
		if (index < 0 || index > cursor.getLength()) {
			try {
				throw new Throwable("超出索引范围!");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return;
		}
		
		Node<T> tempNode = getLocalNode(index);
		cursor = tempNode;
	}

	@Override
	public void back() {
		if (cursor.getParentNode() == null) {
			try {
				throw new Throwable("没有父母结点!");
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return;
		}
		
		cursor = cursor.getParentNode();
		
	}

}
